package servlet;

import com.google.gson.Gson;
import impl.ComRunCpp;
import impl.SaveFileCpp;
import impl.SaveFileTxt;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/CppFileServlet")
public class CppFileServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sourceName = request.getParameter("sourceName");
        String sourceText = request.getParameter("sourceText");
        String inputText = request.getParameter("inputText");
        long time = System.currentTimeMillis();
        SaveFileCpp cpp = new SaveFileCpp(sourceName + time, sourceText);
        SaveFileTxt txt = new SaveFileTxt(sourceName + time, inputText);
        ComRunCpp cr = new ComRunCpp(sourceName + time);
        String result = cr.comRun();
        //Gson gson = new Gson();
        response.setContentType("text/html;charset=utf-8");
        //String json = gson.toJson(result);
        response.getWriter().println(result);
        response.getWriter().close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
